package com.egg.ylt.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_VideoNews;
import com.egg.ylt.pojo.ActionVideoEntity;
import com.egg.ylt.presenter.impl.VideoPlayPresenterImpl;
import com.egg.ylt.view.IVideoPlayView;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;

import butterknife.BindView;

public class ACT_VideoPlaying extends BaseActivity<VideoPlayPresenterImpl> implements IVideoPlayView {


    @BindView(R.id.tv_title_video)
    TextView tvTitleVideo;
    @BindView(R.id.iv_collect_video)
    ImageView ivCollectVideo;
    @BindView(R.id.list_recommend_video)
    RecyclerView listRecommendVideo;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;
    @BindView(R.id.video_player)
    StandardGSYVideoPlayer videoPlayer;

    private String id;
    private ADA_VideoNews newsAdapter;
    private boolean collectType;

    //视频播放相关
    OrientationUtils orientationUtils;

    @Override
    protected void getBundleExtras(Bundle extras) {
        id = extras.getString("id");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_video_playing;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {

        //初始化VideoView
//        initVideoView();

        //初始化recycle
        initRecycler();

        //收藏按钮监听事件
        ivCollectVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isEmpty(Constants.TOKEN)) {
                    readyGo(ACT_UserLogin.class);
                    return;
                }
                if (collectType) {
                    mPresenter.setCancelMessage(Constants.TOKEN, id);
                } else {
                    mPresenter.setCollectMessage(Constants.TOKEN, id, Constants.COMPANYID);
                }
            }
        });
    }

    private void initRecycler() {
        //设置布局管理器
        LinearLayoutManager mLinearLayoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        listRecommendVideo.setLayoutManager(mLinearLayoutManager2);
        //设置适配器
        newsAdapter = new ADA_VideoNews(mContext);
        listRecommendVideo.setAdapter(newsAdapter);
        //item监听
        newsAdapter.setOnItemClickListener(new ADA_VideoNews.OnItemClickListener() {
            @Override
            public void onItemClick(String id, String type) {
                Intent mBundle = new Intent();
                mBundle.putExtra("id", id);
                readyGo(ACT_VideoPlaying.class, mBundle);
            }
        });
        //请求数据
        mPresenter.getActionVideo(Constants.TOKEN, id);
    }

    /**
     * 设置视频播放
     *
     * @param uri
     */
    private void initVideoView(String uri) {
        //关联播放控制器
        String url = "http://9890.vod.myqcloud.com/9890_9c1fa3e2aea011e59fc841df10c92278.f20.mp4";
        // 设置视频链接
        videoPlayer.setUp(uri, true, "");
        //设置旋转
        orientationUtils = new OrientationUtils(this, videoPlayer);
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orientationUtils.resolveByClick();
            }
        });
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置全屏返回按键和缩小功能
        videoPlayer.setBackFromFullScreenListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orientationUtils.backToProtVideo();
            }
        });
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        videoPlayer.startPlayLogic();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoPlayer != null) {
            videoPlayer.onVideoPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (videoPlayer != null) {
            videoPlayer.onVideoResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (orientationUtils != null) {
            orientationUtils.releaseListener();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //如果旋转了就全屏
        videoPlayer.onConfigurationChanged(this, newConfig, orientationUtils, true, true);
    }

    @Override
    public void onBackPressed() {
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        //请求数据
        mPresenter.getActionVideo(Constants.TOKEN, intent.getStringExtra("id"));
    }

    @Override
    public void getVideoData(ActionVideoEntity entity) {
        if (entity == null) {
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无相关咨询");
            return;
        }
        //0是未收藏  1是收藏
        String getCollect = entity.getGetCollect();
        if ("0".equals(getCollect)) {
            collectType = false;
            ivCollectVideo.setSelected(false);
        } else {
            collectType = true;
            ivCollectVideo.setSelected(true);
        }
        toggleShowEmpty(false, -1, "");
        tvTitleVideo.setText(entity.getTitle());
        //设置视频播放链接
        initVideoView(entity.getVideoUrl());

        //设置相关推荐
        if (!ListUtil.isListEmpty(entity.getList())) {
            newsAdapter.addList(entity.getList());
        }

    }

    @Override
    public void setCollectMessage() {
        collectType = true;
        ivCollectVideo.setSelected(true);
    }

    @Override
    public void setCancelMessage() {
        collectType = false;
        ivCollectVideo.setSelected(false);
    }
}
